﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using ReflectionStudio.Core.Database;
using DSTGBMOV;
using ReflectionStudio.Core.FTPClient;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for DSTGBMOV.xaml
    /// </summary>
    public partial class DSTGBMOV : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        DataTable tableUI;
        private DataRow dwUI;
        DSTGBMOVData m_Data, m_DataX;
        public SETVV setUI
        {
            get
            {
                return (SETVV)this.TryFindResource("SETUI");
            }
        }
        public DataRow dwVKPOS;
        public DataRow dwVVLIEFSCH;
        public DataRow dwCOLOR;
        public DataRow dwWBBST;
        public DataSet dsParamaters;
        DSTGBMOVReport m_rpt;
        ArrayList listVV06, ListColor, listBatchNo;
        #endregion
        public DSTGBMOV()
        {
            InitializeComponent();
            this.Name = "发纱报表";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new DSTGBMOVData(dicParamater);
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("ORDER", typeof(string));
            tableUI.Columns.Add("WBSRTDATE", typeof(string));
            tableUI.Columns.Add("WBENTDATE", typeof(string));
            txtBMONTH.Text = m_Data.Right("00" + DateTime.Now.Month.ToString(), 2);
            txtBYEAR.Text = DateTime.Now.Year.ToString();
            txtBDAY.Text = DateTime.Now.Day.ToString();
            txtEMONTH.Text = m_Data.Right("00" + DateTime.Now.Month.ToString(), 2);
            txtEYEAR.Text = DateTime.Now.Year.ToString();
            txtEDAY.Text = DateTime.Now.Day.ToString();
            dwUI = tableUI.NewRow();
            SetUpFtp();
            btnLogFTP_Click(null, null);
            /*
            txtVKPAUFKRS.Text = "Z ";
            txtVKPAUFLFD.Text = "002030";
             * */

        }
        public void btnLogFTP_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string ftpsite = txtFTPSite.Text.Trim();
                string ftpport = txtFTPPort.Text.Trim();
                string ftpuser = txtFTPUser.Text.Trim();
                CheckFtp(ftpsite, ftpport, ftpuser, txtFTPPwd, IsBelongs2Admin.IsChecked == true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show("FTP登录信息有误。请联系你们的破管理员。");
            }
            finally
            {
                ftpClient.Close();
            }
        }
  
        private void btnQuery_Click(object sender, EventArgs e)
        {
            string PrintType = "JustExcel";
            GenerateReport(PrintType);
        }
        private void GenerateReport(string PrintType)
        {
            try
            {
                m_rpt = new DSTGBMOVReport(dicParamater);
                m_rpt.dsSource = setUI;
                m_rpt.PrintType = PrintType;
                m_rpt.ftpClient = ftpClient;
                if (m_Data.exAgent._app == null)
                {
                    m_Data.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
                }
                m_rpt.proxy = this.proxy;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.exAgent = m_Data.exAgent;
                m_rpt.WriteExcelReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }

        

        private void btnGetGBMOV_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //m_Data.UpdateWastamm();
                //return;
                setUI.Clear();
                setUI.AcceptChanges();
                dwUI.Delete();
                string bYear = txtBYEAR.Text.ToString();
                string bMonth = m_Data.Right("00" + txtBMONTH.Text.ToString(), 2);
                string bDay = m_Data.Right("00" + txtBDAY.Text.ToString(), 2);
                string eYear = txtEYEAR.Text.ToString();
                string eMonth = m_Data.Right("00" + txtEMONTH.Text.ToString(), 2);
                string eDay = m_Data.Right("00" + txtEDAY.Text.ToString(), 2);
                string orderno = txtORDER.Text.ToString().Trim();
                string WBSRTDATE = bYear
                    + m_Data.Right("00" + bMonth, 2)
                    + m_Data.Right("00" + bDay.ToString(), 2);
                string WBENTDATE = eYear
                    + m_Data.Right("00" + eMonth, 2)
                    + m_Data.Right("00" + eDay.ToString(), 2);
                if (orderno == "")
                {
                    orderno = "aaaaaaaa";

                };              
                
                dwUI["WBSRTDATE"] = WBSRTDATE;
                dwUI["WBENTDATE"] = WBENTDATE;
                dwUI["ORDER"] = orderno;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    dsResult = m_Data.BuildData(dwUI);
                    setUI.Merge(dsResult);
                    dlgGBMOV.DataContext = setUI.Tables["GBMOV"];

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
